package io.lckgroup.modules.lck.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.lckgroup.common.utils.PageUtils;
import io.lckgroup.common.utils.Query;
import io.lckgroup.modules.lck.dao.CommonCommentDao;
import io.lckgroup.modules.lck.dao.OrderMainDao;
import io.lckgroup.modules.lck.dao.ShopMerchantInfoDao;
import io.lckgroup.modules.lck.entity.CommonCommentEntity;
import io.lckgroup.modules.lck.entity.CustomerInfoEntity;
import io.lckgroup.modules.lck.entity.OrderMainEntity;
import io.lckgroup.modules.lck.entity.ShopMerchantInfoEntity;
import io.lckgroup.modules.lck.form.wxgzh.CommentWxgzhForm;
import io.lckgroup.modules.lck.form.wxgzh.MerchantComment;
import io.lckgroup.modules.lck.service.CommonCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Map;


@Service("commonCommentService")
public class CommonCommentServiceImpl extends ServiceImpl<CommonCommentDao, CommonCommentEntity> implements CommonCommentService {

    @Autowired
    private CommonCommentDao commonCommentDao;
    @Autowired
    private OrderMainDao orderMainDao;
    @Autowired
    private ShopMerchantInfoDao shopMerchantInfoDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CommonCommentEntity> page = this.page(
                new Query<CommonCommentEntity>().getPage(params),
                new QueryWrapper<CommonCommentEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPageByEntity(Map<String, Object> params, CommonCommentEntity commonCommentEntity) {
        IPage<CommonCommentEntity> page = this.page(
                new Query<CommonCommentEntity>().getPage(params),
                new QueryWrapper<CommonCommentEntity>(commonCommentEntity)
        );

        return new PageUtils(page);
    }

    @Transactional
    @Override
    @CacheEvict(value = "weixinGetOrderList", key = "#commonCommentEntity.merchantId")
    public int createComment(CommonCommentEntity commonCommentEntity) {
//        订单ID
        OrderMainEntity orderMainEntity = new OrderMainEntity();
        orderMainEntity.setId(commonCommentEntity.getOrderMainId());
        QueryWrapper<OrderMainEntity> wrapper = new QueryWrapper<>();
        wrapper.setEntity(orderMainEntity);
//        状态值
        OrderMainEntity entity = new OrderMainEntity();
        entity.setIsPay(4);
//        修改订单状态
        int update = orderMainDao.update(entity, wrapper);
        int result = commonCommentDao.insert(commonCommentEntity);
//        根据id查询商家信息
        ShopMerchantInfoEntity shopMerchantInfoEntity = shopMerchantInfoDao.selectById(commonCommentEntity.getMerchantId());
        //计算商家的评分和评论数
        BigDecimal score = shopMerchantInfoEntity.getScore().multiply(new BigDecimal(shopMerchantInfoEntity.getCommentNum().toString()));
        BigDecimal score1 = score.add(commonCommentEntity.getScore());
        BigDecimal score2 = score1.divide(new BigDecimal(shopMerchantInfoEntity.getCommentNum() + 1), 1, BigDecimal.ROUND_HALF_UP);
        Integer commentNum = shopMerchantInfoEntity.getCommentNum() + 1;
        //更新商家的评分和评论数
        QueryWrapper<ShopMerchantInfoEntity> queryWrapper = new QueryWrapper<>();
        ShopMerchantInfoEntity entity1 = new ShopMerchantInfoEntity();
        entity1.setCommentNum(commentNum);
        entity1.setScore(score2);
        queryWrapper.eq("id", commonCommentEntity.getMerchantId());
        int update1 = shopMerchantInfoDao.update(entity1, queryWrapper);

        return result;
    }

    /**
     * 根据审核状态分页查询评论
     *
     * @param params（包含分页参数和审核状态）
     * @param customerInfoEntity
     * @return
     */
    @Override
    public PageUtils getMyCommentByState(Map<String, Object> params, CustomerInfoEntity customerInfoEntity) {
        //分页参数
        Page page = new Page<>(Integer.valueOf(params.get("page").toString()), Integer.valueOf(params.get("limit").toString()));
        //审核状态参数
        String auditState = params.get("auditState").toString();
        IPage<CommentWxgzhForm> iPage = commonCommentDao.getMyCommentByState(page, auditState, customerInfoEntity.getId());
        return new PageUtils(iPage);
    }

    @Override
    public PageUtils merchantCommentList(Map<String, Object> params) {
//        IPage<CommonCommentEntity> page = this.page(
//                new Query<CommonCommentEntity>().getPage(params),
//                new QueryWrapper<CommonCommentEntity>().eq("audit_state",0).eq("merchant_id",params.get("merchantId"))
//        );
        //分页参数
        Page page = new Page<>(Integer.valueOf(params.get("page").toString()), Integer.valueOf(params.get("limit").toString()));
        //商家id
        String merchantId =params.get("merchantId").toString();
        IPage<MerchantComment> iPage = commonCommentDao.merchantCommentList(page , merchantId);
        return new PageUtils(iPage);
    }
}